
class Triangle{
public:
    Vertex* A,* B,* C, G;
    struct Normal{
        float x,y,z;
    }normal;
    Uint32 color;
    Triangle(Vertex* _A,Vertex* _B,Vertex* _C,Uint32 _color):
        A(_A),B(_B),C(_C),color(_color){
        G.merge(
            (A->x+B->x+C->x)/3,
            (A->y+B->y+C->y)/3,
            (A->z+B->z+C->z)/3
        );
        updateNormal();

    };
    void updateNormal(){
        //边上的向量AB和向量AC的正交分解分量
        float bx=B->x-A->x;        float cx=C->x-A->x;
        float by=B->y-A->y;        float cy=C->y-A->y;
        float bz=B->z-A->z;        float cz=C->z-A->z;
        //叉乘得到垂直于平面的法向量
        normal.x=by*cz-bz*cy;
        normal.y=bz*cx-bx*cz;
        normal.z=bx*cy-by*cx;
        //单位化,防止太大太小可能会导致的溢出或精度不够
        float n=sqrt(normal.x*normal.x+normal.y*normal.y+normal.z*normal.z);
        normal.x/=n;
        normal.y/=n;
        normal.z/=n;
    };
    void draw(SDL_Renderer* render,SDL_Surface* ScSurface,Camara camara){
        float dtx =G.x - camara.x;
        float dty =G.y - camara.y;
        float dtz =G.z - camara.z;
        float viewDotMul=normal.x*dtx+normal.y*dty+normal.z*dtz;
        if (viewDotMul<=0){return;}
        // Vertex G_(G.x+normal.x,G.y+normal.y,G.z+normal.z);
        // Line g(&G,&G_,0xffffff);g.draw(render,camara);
        Uint32* ScPixels=static_cast<Uint32*>(ScSurface->pixels);
        int scw=ScSurface->w,sch=ScSurface->h;

        A->WorldToCamara(camara);
        B->WorldToCamara(camara);
        C->WorldToCamara(camara);
        int minx=std::max(std::min(std::min((int)A->scx,(int)B->scx),(int)C->scx),0);
        int miny=std::max(std::min(std::min((int)A->scy,(int)B->scy),(int)C->scy),0);
        int maxx=std::min(std::max(std::max((int)A->scx,(int)B->scx),(int)C->scx),scw);
        int maxy=std::min(std::max(std::max((int)A->scy,(int)B->scy),(int)C->scy),sch);
        for(int scy=miny;scy<=maxy;scy++){
            for(int scx=minx;scx<=maxx;scx++){
                if(!(0<scx&&scx<scw))continue;
                if(!(0<scy&&scy<sch))continue;
                if(!(A->drawable()||B->drawable()||C->drawable()))continue;
                int bscx=B->scx-A->scx;
                int bscy=B->scy-A->scy;
                int cscx=C->scx-A->scx;
                int cscy=C->scy-A->scy;
                int mscx=scx-A->scx;
                int mscy=scy-A->scy;
                float dved=bscx*cscy-bscy*cscx;
                float w1sc=(mscx*cscy-mscy*cscx)/dved;
                float w2sc=(mscy*bscx-mscx*bscy)/dved;
                float w0sc=1.0-w1sc-w2sc;
                if(!(w0sc>=-0.01&&w1sc>=-0.05&&w2sc>=-0.01))continue;
                // if(!(w0sc>=0.0&&w1sc>=0.0&&w2sc>=0.0))continue;
                float scz=w0sc*A->scz+w1sc*B->scz+w2sc*C->scz;
                if(scz>=Z_buffer[scy*SCREEN_W+scx]){
                    ScPixels[(scy*scw+scx)]=color;
                    // ScPixels[(scy*scw+scx)]=(int(0xFF*scz))%0x100*0x00010101;
                    Z_buffer[scy*SCREEN_W+scx]=scz;
                }
            }
        }
    }
};